package com.ruoyi.train.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.train.domain.Vo.StudentInformationVo;
import com.ruoyi.train.domain.StudentProcessRecord;
import com.ruoyi.train.domain.TrainClass;
import com.ruoyi.train.mapper.StudentProcessRecordMapper;
import com.ruoyi.train.mapper.TrainClassMapper;
import com.ruoyi.train.service.IStudentProcessRecordService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import com.ruoyi.train.domain.StudentInformation;
import com.ruoyi.train.service.IStudentInformationService;

import org.springframework.web.multipart.MultipartFile;

/**
 * 学员信息Controller
 * 
 * @author wusihao
 * @date 2023-03-21
 */
@CrossOrigin
@Api(tags = "学生信息", value = "学生信息接口")
@RestController
@RequestMapping("/student/information")
public class StudentInformationController extends BaseController
{
    @Autowired
    private IStudentInformationService studentInformationService;
    @Autowired
    private IStudentProcessRecordService studentProcessRecordService;

    @Autowired
    private StudentProcessRecordMapper studentProcessRecordMapper;

    @Autowired
    private TrainClassMapper trainClassMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询学员信息列表
     */
    @ApiOperation("查询学生信息")
    @GetMapping("/list/{classId}")
    public TableDataInfo list(@PathVariable Long classId)
    {
        startPage();
        List<StudentInformationVo> studentInformationVos = new ArrayList<>();
        List<StudentInformation> list = studentInformationService.selectStudentByClassId(classId);
        for (StudentInformation studentInformation : list) {
            String serialNumber = studentInformation.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
            StudentProcessRecord studentProcessRecord = studentProcessRecordMapper.selectOne(studentProcessRecordQueryWrapper);
            String trainConclusion = studentProcessRecord.getTrainConclusion();
            StudentInformationVo studentInformationVo = new StudentInformationVo();
            BeanUtils.copyProperties(studentInformation, studentInformationVo);
            studentInformationVo.setTrainConclusion(trainConclusion);
            studentInformationVos.add(studentInformationVo);
        }
        return getDataTable(studentInformationVos);
    }


    /**
     * 获取学员信息详细信息
     */
    @ApiOperation("获取学生详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        StudentInformationVo studentInformationVo = studentInformationService.selectStudentInformationById(id);

        return success(studentInformationVo);
    }

    /**
     * 新增学员信息
     */
    @ApiOperation("新增学生信息")
    @Log(title = "学员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentInformation studentInformation)
    {
        return toAjax(studentInformationService.insertStudentInformation(studentInformation));
    }

    /**
     * 修改学员信息
     */
    @ApiOperation("修改学生信息")
    @Log(title = "学员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentInformation studentInformation)
    {
        return toAjax(studentInformationService.updateStudentInformation(studentInformation));
    }

    /**
     * 删除学员信息
     */
    @ApiOperation("删除学生信息")
    @Log(title = "学员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentInformationService.deleteStudentInformationByIds(ids));
    }

    /**
     * 提交考试结论
     */
    @ApiOperation("提交考试结论")
    @Log(title = "提交考试结论", businessType = BusinessType.DELETE)
    @PostMapping("/submitConclusion")
    public AjaxResult subExamConclusion(@RequestBody StudentInformationVo studentInformationVo)
    {
        return toAjax(studentProcessRecordService.submitExamConclusion(studentInformationVo));
    }

    /**
     * 得到班级附件信息
     */
    @ApiOperation("得到班级附件信息")
    @Log(title = "得到班级附件信息", businessType = BusinessType.DELETE)
    @GetMapping("/getInformationClass/{classId}")
    public AjaxResult subExamConclusion( @PathVariable Long classId)
    {
        TrainClass trainClass = trainClassMapper.selectTrainClassById(classId);
        return success(trainClass);
    }

    /**
     * 提交班级附件信息
     *
     * */
    @ApiOperation("提交班级附件信息")
    @Log(title = "提交班级附件信息", businessType = BusinessType.DELETE)
    @PostMapping("/submitClassInfo")
    public AjaxResult submitClassInfo(@RequestBody TrainClass classInfo)
    {
        return toAjax(studentInformationService.submitClassInfo(classInfo));
    }

    /**
     * 是否确实通过考试申请
     * */
    @ApiOperation("是否确实通过考试申请")
    @Log(title = "是否确实通过考试申请", businessType = BusinessType.DELETE)
    @GetMapping("/confirmTrainisPassAll/{classId}")
    public AjaxResult confirmTrainisPassAll(@PathVariable Long classId)
    {
        return toAjax(studentInformationService.confirmTrainisPassAll(classId));
    }

    /**
     * 导入学生信息
     */
    @Log(title = "学生信息导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Long classId, Long trainId) throws Exception
    {
        ExcelUtil<StudentInformation> util = new ExcelUtil<StudentInformation>(StudentInformation.class);
        List<StudentInformation> studentInformationList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = studentInformationService.importStudentInformation(studentInformationList, operName, classId, trainId);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<StudentInformation> util = new ExcelUtil<StudentInformation>(StudentInformation.class);
        util.importTemplateExcel(response, "学生信息模板");
    }
}
